package com.hua.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hua.common.CustomError;
import com.hua.entities.*;
import com.hua.service.SetmealDtoService;
import com.hua.service.serviceImpl.CategoryServiceImpl;
import com.hua.service.serviceImpl.SetmealDishServiceImpl;
import com.hua.service.serviceImpl.SetmealDtoServiceImpl;
import com.hua.service.serviceImpl.SetmealServiceImpl;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealServiceImpl setmealService;

    @Autowired
    private SetmealDishServiceImpl service;

    @Autowired
    private CategoryServiceImpl categoryService;

    @PostMapping
    public R<String> getSetMealInfo(@RequestBody  SetmealDto dto){
        setmealService.savaSetmealAndDish(dto);
        return R.success("添加成功~~");
    }

    @GetMapping("/page")
    public R<Page> getPageInfo(int page,int pageSize,String name){
        Page<Setmeal> getPage
                = new Page<>(page,pageSize);
        Page<SetmealDto> getPageInfo
                = new Page<>();

        BeanUtils.copyProperties(getPage,getPageInfo,"records");

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(name!=null,Setmeal::getName,name);

        Page<Setmeal> page1 = setmealService.page(getPage);
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> recodsCopyAndAdd = null;

        recodsCopyAndAdd =  records.stream().map(item->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Category getName = categoryService.getById(item.getCategoryId());
            setmealDto.setCategoryName(getName.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        getPageInfo.setRecords(recodsCopyAndAdd);

        return R.success(getPageInfo);
    }
    @DeleteMapping
    public R<String> deleteById(@RequestParam("ids") List<Long>ids){
        LambdaQueryWrapper<Setmeal>lambdaQueryWrapper
                = new LambdaQueryWrapper();

        lambdaQueryWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        int count = setmealService.count(lambdaQueryWrapper);
        if(count>0){
            throw new CustomError("套餐在售卖中，不能删除");
        }
        setmealService.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish>lambdaQueryWrapper1
                = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(SetmealDish::getId,ids);
        service.remove(lambdaQueryWrapper1);
        return R.success("删除成功~~~");
    }
    @PostMapping("/status/{id}")
    public R<String> updateStatus(@PathVariable Long id,@RequestParam("ids") List <Long> ids){
        LambdaUpdateWrapper<Setmeal> lambdaUpdateWrapper
                = new LambdaUpdateWrapper<>();
        if(id==0L){
            lambdaUpdateWrapper.set(Setmeal::getStatus,0);
        }else {
            lambdaUpdateWrapper.set(Setmeal::getStatus,1);
        }
        for (int i = 0 ; i< ids.size() ; i++){
            /*
            这里不能用eq 因为我们不能清除上一次存进去的id，
            如果用了 查询语句就会变成
            第一次
            set xxx where id = ?
            第二次
            set xxx where id = ? and id= ? ;这肯定不成立- -

            * */
            lambdaUpdateWrapper.in(Setmeal::getId,ids);
            setmealService.update(lambdaUpdateWrapper);
        }

        return R.success("修改成功");
    }
    @GetMapping("/list")
    public R<List<Setmeal>> getStemealInfo(Setmeal setmeal){
         LambdaQueryWrapper<Setmeal>wrapper
                 =new LambdaQueryWrapper<>();
         wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
         wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());

        List<Setmeal> list = setmealService.list(wrapper);

        return R.success(list);

    }
}
